/*
网某个设备写入若干字节，非阻塞IO方式
*/

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
    if (argc != 3)
    {
        printf("usage:%s <dev> <bytes>\n", argv[0]);
        return -1;
    }
    const char *dev = argv[1];
    const char *str = argv[2];
    size_t n = strlen(str);

    int fd = open(dev, O_CLOEXEC | O_WRONLY | O_NONBLOCK);
    if (fd < 0)
    {
        printf("open error(%s)\n", strerror(errno));
        return -1;
    }
    printf("open %s succeed,fd=%d\n", dev, fd);

    ssize_t write_n = write(fd, str, n);
    if (write_n < 0)
    {
        printf("write error(%s)\n", strerror(errno));
        close(fd);
        return -1;
    }
    printf("write succeed,write_n=%ld\n", write_n);

    close(fd);

    return 0;
}